//
//  UIImage+Extension.swift
//  SinaWeibo
//
//  Created by 李江波 on 2016/11/20.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

import UIKit

extension UIImage {
    //截取屏幕
    class func snapShotCurrentWindow() -> UIImage {
    
        let window = UIApplication.shared.keyWindow!
        //开启图片的上下文
        UIGraphicsBeginImageContextWithOptions(window.frame.size, true, 0)
        //将当前的window绘制到图形的上下文
        window.drawHierarchy(in: window.frame, afterScreenUpdates: true)
        //从图片的上下文中获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭图片的上下文
        UIGraphicsEndImageContext()
        
        return image!
    }
    
    //缩放图片
    func scale(width: CGFloat) -> UIImage {
        
        if self.size.width < width {
            return self
        }
        
        let H = self.size.height / self.size.width * width
        
        let imgBounds = CGRect(x: 0, y: 0, width: width, height: H)
        
        //开启图片的上下文
        UIGraphicsBeginImageContextWithOptions(imgBounds.size, true, 0)
        //将当前的image绘制到图形的上下文
        self.draw(in: imgBounds)
        //从图片的上下文中获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return image!
    }

}
